/**
 * 
 */
package br.com.skynix.tcmc.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import org.apache.commons.lang3.StringUtils;

import br.com.skynix.tcmc.model.enumeration.TipoContaFinanceiroEnum;

/**
 * @author davi
 * 
 */
@FacesConverter(value = "TipoFinanceiro_Converter")
public class TipoContaFinanceiroConverter implements Converter {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext
	 * , javax.faces.component.UIComponent, java.lang.String)
	 */
	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
		if (StringUtils.isNotBlank(value)) {
			return TipoContaFinanceiroEnum.getTipoConta(value);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext
	 * , javax.faces.component.UIComponent, java.lang.Object)
	 */
	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object tipo) {
		if (tipo != null
				&& TipoContaFinanceiroEnum.class.equals(tipo.getClass())) {
			return ((TipoContaFinanceiroEnum) tipo).getCodigo();
		}
		return "";
	}
}
